﻿using System;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Microsoft.Extensions.DependencyInjection;
using JESAI.Core.Util.Helpers;

namespace JESAI.Core;

internal class AplicationWithExternalServiceProvider : IApplicationWithExternalServiceProvider
{
    public AplicationWithExternalServiceProvider(
        [NotNull] Type startupModuleType,
        [NotNull] IServiceCollection services,
        [CanBeNull] Action<ApplicationCreationOptions> optionsAction
        ) 
    {
        services.AddSingleton<IApplicationWithExternalServiceProvider>(this);
    }

    void IApplicationWithExternalServiceProvider.SetServiceProvider([NotNull] IServiceProvider serviceProvider)
    {
        Check.NotNull(serviceProvider, nameof(serviceProvider));

        //if (ServiceProvider != null)
        //{
        //    if (ServiceProvider != serviceProvider)
        //    {
        //        throw new Exception("Service provider was already set before to another service provider instance.");
        //    }

        //    return;
        //}

        //SetServiceProvider(serviceProvider);
    }

    public async Task InitializeAsync(IServiceProvider serviceProvider)
    {
        Check.NotNull(serviceProvider, nameof(serviceProvider));

        //SetServiceProvider(serviceProvider);

        //await InitializeModulesAsync();
    }

    public void Initialize([NotNull] IServiceProvider serviceProvider)
    {
        Check.NotNull(serviceProvider, nameof(serviceProvider));

        //SetServiceProvider(serviceProvider);

        //InitializeModules();
    }

    //public override void Dispose()
    //{
    //    base.Dispose();

    //    if (ServiceProvider is IDisposable disposableServiceProvider)
    //    {
    //        disposableServiceProvider.Dispose();
    //    }
    //}
}
